package cn.rokhdelar.ams.management.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.rokhdelar.ams.common.domain.vo.PageVO;
import cn.rokhdelar.ams.management.domain.po.SysAnnouncement;
import cn.rokhdelar.ams.management.domain.query.AnnouncementQuery;
import cn.rokhdelar.ams.management.domain.vo.AnnouncementVO;
import cn.rokhdelar.ams.management.service.SysAnnouncementService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/management/announcements")
@Schema(title = "公告接口", description = "公告接口")
@RequiredArgsConstructor
public class AnnouncementController {
    private final SysAnnouncementService announcementService;

    @GetMapping
    @Operation(summary = "根据条件分页查询公告")
    public ResponseEntity<PageVO<AnnouncementVO>> getAnnouncementsByPage(@ParameterObject AnnouncementQuery query) {
        return ResponseEntity.ok(announcementService.getAnnouncementsByPage(query));
    }

    @GetMapping("/{id}")
    @Operation(summary = "根据ID查询公告")
    public ResponseEntity<AnnouncementVO> getAnnouncement(@PathVariable String id) {
        return ResponseEntity.ok(announcementService.getAnnouncement(id));
    }

    @PostMapping
    @Operation(summary = "创建新的公告信息")
    public ResponseEntity<Boolean> createAnnouncement(@RequestBody AnnouncementVO announcementVO) {
        SysAnnouncement sysAnnouncement = BeanUtil.copyProperties(announcementVO, SysAnnouncement.class);
        return ResponseEntity.ok(announcementService.save(sysAnnouncement));
    }

    @PatchMapping
    @Operation(summary = "根据ID更新公告信息")
    public ResponseEntity<Boolean> updateAnnouncement(@RequestBody AnnouncementVO announcementVO) {
        SysAnnouncement sysAnnouncement = BeanUtil.copyProperties(announcementVO, SysAnnouncement.class);
        return ResponseEntity.ok(announcementService.saveOrUpdate(sysAnnouncement));
    }

    @DeleteMapping(value = "/{id}")
    @Operation(summary = "根据ID删除公告")
    public ResponseEntity<Boolean> deleteAnnouncement(@PathVariable String id) {
        return ResponseEntity.ok(announcementService.removeById(id));
    }

}
